unit goalkeeperUnit;

interface

uses common;

type
  GoalKeeper = class
    function move_before(timeLeft, goalKeeperPosition, playerDirection : integer) : integer;
    function move_after(timeLeft, goalKeeperPosition, hitDirection, ballY : integer) : integer;
  end;
var
  left, jumps, kol:integer;
  flag: boolean;
implementation

{ GoalKeeper }

function GoalKeeper.move_after(timeLeft, goalKeeperPosition, hitDirection, ballY: integer): integer;
begin
  if (hitDirection+HANDS>= goalKeeperPosition) and (hitDirection-HANDS<= goalKeeperPosition) then
  begin
    move_after := HANDS;
  end else begin
    if (hitDirection > goalKeeperPosition) then
    begin
      if (2*GOALKEEPER_WIDTH*(DISTANCE/STRENGTH-left+1) + goalKeeperPosition >= hitdirection-HANDS) and (2*GOALKEEPER_WIDTH*(DISTANCE/STRENGTH+left+1) + goalKeeperPosition <= hitdirection+HANDS) then
      begin
        move_after := STEP_RIGHT;
        if (goalKeeperPosition >= hitdirection-HANDS) and (goalKeeperPosition <= hitdirection+HANDS) then
        begin
          move_after := HANDS;
        end;
      end else begin
        if (flag=false) then
        begin
          if (hitdirection<=goalkeeperposition+goalkeeper_length+DISTANCE/STRENGTH*jump_length) and (hitdirection>=goalkeeperposition+goalkeeper_length+DISTANCE/STRENGTH*jump_length) then
          begin
            move_after:=JUMP_RIGHT;
          end else begin
            move_after:=STAY;
            flag:=true;
          end;
        end else begin
          move_after:=jump_right;
        end;
      end;
    end else if (hitDirection < goalKeeperPosition) then
    begin
      if (goalKeeperPosition - 2*GOALKEEPER_WIDTH*(DISTANCE/STRENGTH-left+1) >= hitdirection-HANDS) and (goalKeeperPosition - 2*GOALKEEPER_WIDTH*(DISTANCE/STRENGTH+left+1) <= hitdirection+HANDS) then
      begin
        move_after := STEP_LEFT;
        if (goalKeeperPosition >= hitdirection-HANDS) and (goalKeeperPosition <= hitdirection+HANDS) then
        begin
          move_after := HANDS;
        end;
      end else begin
        if (flag=false) then
        begin
          if (hitdirection<=goalkeeperposition+goalkeeper_length+DISTANCE/STRENGTH*jump_length) and (hitdirection>=goalkeeperposition+goalkeeper_length+DISTANCE/STRENGTH*jump_length) then
          begin
            move_after:=JUMP_left;
          end else begin
            move_after:=STAY;
            flag:=true;
          end;
        end else begin
          move_after:=jump_left;
        end;
      end;
    left:=left+1;
  end;
end;
end;
function GoalKeeper.move_before(timeLeft, goalKeeperPosition, playerDirection: integer): integer;
begin
  flag:=false;
  left:=0;
  if playerDirection > goalKeeperPosition then
  begin
    move_before:= STEP_RIGHT;
  end else if playerDirection < goalKeeperPosition then
  begin
    move_before:= STEP_LEFT;
  end else begin
    move_before:= STAY;
  end;
end;

end.
